package com.example.easypad;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

public class activity_item extends AppCompatActivity {
    private String type;
    private Spinner sp_type;
    private ImageButton ib_back;
    private int index;
    private String id;
    String context,title;
    private EditText et_context;
    private EditText et_title;
    private String username, password, sql = "";
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        initLayout();
        index = Integer.parseInt(getIntent().getStringExtra("index"));
        id = getIntent().getStringExtra("id");
        title = getIntent().getStringExtra("title");
        context = getIntent().getStringExtra("context");
        type = getIntent().getStringExtra("type");
        et_title.setText(title);
        et_context.setText(et_context.getText() + context);
        db = MainActivity.db;
        initSpinner();
        //SpannableString富文本类
//        SpannableString ss = new SpannableString(title + "\n" + context);
//        ss.setSpan(new AbsoluteSizeSpan(60),0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        ss.setSpan(new AbsoluteSizeSpan(40),4,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);





        sp_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//                Toast.makeText(activiti_new_Edit.this,position + "",Toast.LENGTH_SHORT).show();
                switch (position){
                    case 0 : type = "生活";
                        break;
                    case 1 : type = "工作";
                        break;
                    case 2 : type = "娱乐";
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context = et_context.getText() + "";
                title = et_title.getText() + "";
                if(context.equals("") || title.equals("")) {
                    finish();
                    return;
                }
                //sql = "insert into Notes (title,context,type) values ('" + getTextViewTitle() + "', '" + getTextViewContext() + "', '" + type + "')";
                saveData();
                finish();
            }
        });
    }

//    private String getTextViewTitle() {
//        String text = et_item.getText() + "";
//        return text.split("\n")[0];
//    }
//
//    private String getTextViewContext() {
//        String text = et_item.getText() + "";
//        return text.substring(getTextViewTitle().length());
//    }


    private void saveData() {
        sql = "UPDATE Notes SET title = '" + et_title.getText() + "',context = '" + et_context.getText() + "',type = '" + type + "' WHERE id = '" + id + "' AND userName = '" + MainActivity.userName + "'";
        System.out.println(sql);
        db.execSQL(sql);
        MainActivity.showData();
        MainActivity.myAdapter = new MyAdapter(MainActivity.data,MainActivity.mainContext);
        MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);
//        MainActivity.data.add(new Notes(getTextViewTitle(),getTextViewContext(),type));
//        MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);
    }

    @Override
    public void onBackPressed() {
//        String text = et_item.getText() + "";
//        if(text.equals(title + context)) {
//            finish();
//            return;
//        }
        //sql = "insert into Notes (title,context,type) values ('" + getTextViewTitle() + "', '" + getTextViewContext() + "', '" + type + "')";
        saveData();
        finish();
    }

    private void initSpinner() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_type.setAdapter(adapter);
        switch (type) {
            case  "生活" : sp_type.setSelection(0);
                break;
            case  "工作" : sp_type.setSelection(1);
                break;
            case  "娱乐" : sp_type.setSelection(2);
                break;
        }


    }
    private void initLayout() {
        et_title = findViewById(R.id.et_title);
        et_context = findViewById(R.id.et_context);
        ib_back = findViewById(R.id.ib_back);
        sp_type = findViewById(R.id.sp_type);
    }

}